package com.toKnow.controller;

import com.toKnow.model.entity.Article;
import com.toKnow.model.entity.ArticleAnswer;
import com.toKnow.model.entity.ArticleComment;
import com.toKnow.model.vo.*;
import com.toKnow.service.ArticleService;
import com.toKnow.service.ServiceException;
import com.toKnow.utils.JwtUserSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    public Integer getUid(HttpSession session){
        try{
            String token = (String) session.getAttribute("userToken");
            return Integer.parseInt(JwtUserSessionUtil.getInfo(token,"id"));
        } catch (Exception e){
            return null;
        }
    }

    @ResponseBody
    @RequestMapping(value = "",method = RequestMethod.POST)
    public BasicVo homeSearch(HttpSession session,RestrictionVo restrictionVo){
//        System.out.println(restrictionVo);
//        return new BasicVo(false,"正在测试",null);
        try{
            List<ArticleAbstractVo> list=new ArrayList<>();
            List<Article> articles = articleService.searchArticle(getUid(session),restrictionVo);
            for (Article article : articles){
                list.add(new ArticleAbstractVo(article));
            }
            return new BasicVo(true,null,list);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }

    // transactional right
    @ResponseBody
    @RequestMapping(value = "/content")
    public BasicVo getArticle(HttpSession session,String article_id){
        if (article_id==null) return new BasicVo(false,"没有携带文章编号",null);
        // TestHelpUtil.fakeLogin(session,1);
        try{
            Integer aid=Integer.parseInt(article_id);
            Integer uid=getUid(session);
            Article article = articleService.getArticle(aid);
            if(article==null) return new BasicVo(false,"没有找到文章",null);
            ArticleVo articleVo = new ArticleVo(article);
            if (uid!=null){
                articleService.browseArticle(uid,aid);
                articleVo.setIs_like(articleService.isLikeArticle(uid,aid));
                articleVo.setIs_collect(articleService.isCollectArticle(uid,aid));
            }
            return new BasicVo(true,null,articleVo);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        }catch (NumberFormatException e){
            return new BasicVo(false,"文章编号不为数字",null);
        }
        catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
//    @ResponseBody
//    @RequestMapping(value = "/content/browser")//,method = RequestMethod.POST)
//    public BrowseVO browseArticle(String userid,String articleid){
//        BrowseVO browseVO = new BrowseVO();
//        if (userid==null||articleid==null) return null;
//        Integer uid=Integer.parseInt(userid),aid=Integer.parseInt(articleid);
//        try{
//            browseVO.setIslike(articleService.isLikeArticle(uid,aid));
//            browseVO.setIscollect(articleService.isCollectArticle(uid,aid));
//            articleService.browseArticle(uid,aid);
//            return browseVO;
//        }catch (Exception e){
//            return null;
//        }
//    }
//    @ResponseBody
//    @RequestMapping(value = "/content/like",method = RequestMethod.POST)
//    public BasicVo likeArticle(HttpSession session,String article_id){
//        if (article_id==null) return new BasicVo(false,"没有携带文章编号",null);
//        // TestHelpUtil.fakeLogin(session,1);
//        try{
//            Integer aid=Integer.parseInt(article_id);
//            Integer uid=getUid(session);
//            if (uid!=null){
//                if (articleService.changeLikeArticle(uid,aid)) return new BasicVo(true,null,null);
//                else return new BasicVo(false,"未知错误",null);
//            }else {
//                return new BasicVo(false,"没有找到用户ID，可能是没有登录",null);
//            }
//        }catch (NumberFormatException e){
//            return new BasicVo(false,"文章编号不为数字",null);
//        }catch (ServiceException e){
//            return new BasicVo(false,e.getMessage(),null);
//        }
//        catch (Exception e){
//            return new BasicVo(false,"未知错误",null);
//        }
//    }
    @ResponseBody
    @RequestMapping(value = "/content/collect",method = RequestMethod.POST)
    public BasicVo collectArticle(HttpSession session,String article_id){
        if (article_id==null) return new BasicVo(false,"没有携带文章编号",null);
        // TestHelpUtil.fakeLogin(session,1);
        try{
            Integer aid=Integer.parseInt(article_id);
            Integer uid=getUid(session);
            if (uid!=null){
                if (articleService.changeCollectArticle(uid,aid)) return new BasicVo(true,null,null);
                else throw new RuntimeException("未知错误");
            }else {
                return new BasicVo(false,"没有找到用户ID，可能是没有登录",null);
            }
        }catch (NumberFormatException e){
            return new BasicVo(false,"文章编号不为数字",null);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        } catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
    @RequestMapping(value = "/content/comment",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getComment(Integer article_id,Integer page){
        try {
            if (article_id == null) return new BasicVo(false, "没有携带文章编号", null);
            if (page == null || page < 0) return new BasicVo(false, "页码非法", null);
            List<ArticleComment> comments = articleService.getComment(article_id, page);
            List<CommentVo> ans = new ArrayList<>();
            for (ArticleComment comment : comments) {
                ans.add(new CommentVo(comment));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        } catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
    @RequestMapping(value = "/content/comment/reply",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo getAnswer (Integer comment_id,Integer page){
        try {
            if (comment_id == null) return new BasicVo(false, "没有携带文章编号", null);
            if (page == null || page < 0) return new BasicVo(false, "页码非法", null);
            List<ArticleAnswer> answers = articleService.getAnswer(comment_id, page);
            List<AnswerVo> ans = new ArrayList<>();
            for (ArticleAnswer answer : answers) {
                ans.add(new AnswerVo(answer));
            }
            return new BasicVo(true, null, ans);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        } catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
}
